package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    // 后台查询订单  get请求  参数 page pageSize num  2个时间
    @GetMapping("page")
    public R page(Integer page, Integer pageSize, String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        IPage<Orders> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number != null, Orders::getNumber, number);
//        lqw.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        lqw.le(beginTime != null, Orders::getOrderTime, endTime);
        lqw.ge(endTime != null, Orders::getOrderTime, beginTime);
        lqw.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, lqw);
        return R.success(pageInfo);

    }

    @PutMapping
    public R updateStatus(@RequestBody Orders orders) {

        orderService.updateById(orders);
        return R.success("修改成功");
    }

    @GetMapping("userPage")
    public R page(Integer page, Integer pageSize) {
        IPage<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
        lqw.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, lqw);
        // 以上的基础操作只是单纯的把order对象和表中的数据进行查询,并没有考虑到前端页面显示的用户名称

        Page<OrdersDto> resultPage = new Page<>();
        List<Orders> ordersList = pageInfo.getRecords();
        if (ordersList == null) {
            return R.success(pageInfo);
        }
        List<OrdersDto> dtoList = ordersList.stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(OrderDetail::getOrderId, orders.getId());
            List<OrderDetail> list = orderDetailService.list(lqw1);
            ordersDto.setOrderDetails(list);
            return ordersDto;
        }).collect(Collectors.toList());
        resultPage.setRecords(dtoList);
        resultPage.setTotal(pageInfo.getTotal());
        return R.success(resultPage);
    }

    @PostMapping("again")
    public R again(@RequestBody Orders orders) {
        log.info("再来一单,相同订单内容编号为:{}", orders.getId());
        orderService.again(orders);
        return R.success("ok");
    }

    @PostMapping("cancel")
    public R cancel(@RequestBody Orders orders) {
        log.info("用户取消了订单");
        Long ordersId = orders.getId();
        Orders newOrders = orderService.getById(ordersId);
        newOrders.setStatus(5);
        orderService.updateById(newOrders);
        return R.success("ok");
    }
}